package com.bawei.day18_mediaplayer2.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.RemoteViews;

import com.bawei.day18_mediaplayer2.MyMode;
import com.bawei.day18_mediaplayer2.R;
import com.bawei.day18_mediaplayer2.bean.SongBean;

import java.io.IOException;
import java.util.List;

import androidx.annotation.Nullable;

public class MusicService extends Service {
    private MediaPlayer mediaPlayer = new MediaPlayer();

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBinder();
    }




    public class MusicBinder extends Binder{

        private List<SongBean> list;
        private int position = 0;//当前下标
        private int mode = MyMode.MODE_ORDER;//播放模式

        public void setList(List<SongBean> list) {//设置歌曲资源
          this.list = list;
        }
        public void play(int position){//点击item播放音乐更新下标
            this.position = position;//更新下标
            if(mediaPlayer != null){
                try {
                    mediaPlayer.reset();//重置
                    mediaPlayer.setDataSource(list.get(position).getData());//设置媒体资源
                   // mediaPlayer.prepare();//准备
                    mediaPlayer.prepareAsync();//异步准备，建议采用这个
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {//准备监听
                            mediaPlayer.start();//播放
                            showNotification();

                        }
                    });
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {//播放完毕自动播放下一首
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            next();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
                sendBroadcast(new Intent("c"));

            }
        }
        public void pauseOrstart(){//继续播放+暂停
            if(mediaPlayer != null){
                if(mediaPlayer.isPlaying()){//判断是否正在播放
                    mediaPlayer.pause();//暂停
                }else{
                    mediaPlayer.start();//播放
                }
            }
        }

        public void pause(){//暂停
            if(mediaPlayer != null){
                mediaPlayer.pause();
            }
        }
        public void start(){//继续播放
            if(mediaPlayer != null){
                mediaPlayer.start();
            }
        }

        public void next(){//下一首
            if(mode == MyMode.MODE_ORDER){//顺序
                if(position == list.size()-1){//是否是最后一首歌曲
                    position = 0;
                }else{
                    position++;
                }
            }else if(mode == MyMode.MODE_RANDOM){//随机
                position = (int) (Math.random()*list.size());
            }
            play(position);

        }

        public void pre(){//上一首
            if(mode == MyMode.MODE_ORDER){//顺序
                if(position == 0){//是否是第一首
                    position = list.size()-1;
                }else{
                    position--;
                }
            }else if(mode == MyMode.MODE_RANDOM){//随机
                position = (int) (Math.random()*list.size());
            }
            play(position);

        }

        public void changeMode(int mode){//播放模式的切换
            this.mode = mode;
        }

        public String getImage(){//返回当前歌曲的专辑图片
            return list.get(position).getAlbum();
        }

        public String getName(){//返回当前歌曲的名称
            return list.get(position).getName();
        }

        public long getDuration(){//返回歌曲总时长
            if(mediaPlayer != null){
                return mediaPlayer.getDuration();
            }
            return 0;
        }

        public long getCurrent(){//返回歌曲当前进度
            if(mediaPlayer != null){
                return mediaPlayer.getCurrentPosition();
            }
            return 0;
        }

        public void seekTo(int time){//拖拽进度条的时候播放到指定进度
            if(mediaPlayer != null){
                mediaPlayer.seekTo(time);
            }
        }
        public void showNotification(){//显示通知
            Notification.Builder builder = new Notification.Builder(getApplicationContext());//获得上下文

            builder.setSmallIcon(R.mipmap.ic_launcher);

            RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
            //设置文字
            remoteViews.setTextViewText(R.id.tv,getName());//设置当前歌曲的名称
            //设置图片
            Bitmap bitmap = BitmapFactory.decodeFile(getImage());
            remoteViews.setImageViewBitmap(R.id.iv,bitmap);
            //设置点击
            {
                Intent intent = new Intent();
                intent.setAction("com.bawei.play");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.iv_play, pendingIntent);
            }
            {
                Intent intent = new Intent();
                intent.setAction("com.bawei.next");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.iv_next, pendingIntent);
            }

            {
                Intent intent = new Intent();
                intent.setAction("com.bawei.pre");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.iv_pre, pendingIntent);
            }



            builder.setCustomContentView(remoteViews);
            Notification notification = builder.build();
            startForeground(1,notification);

        }
    }
}
